// pages/moments/moments.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index1:0,
    userId:'',
    page:1,
    isLoading:false,
    recordList:
    [
      
    ]
  },
  //点击图片可以预览
  imgClick(e){
    var index = e.currentTarget.dataset.index
    console.log(index)
    var imgUrl = this.data.recordList[this.data.index1].fileList
    console.log(imgUrl)
    wx.previewImage({
      urls: imgUrl,
      current:imgUrl[index],
      success:(res=>{
        console.log(res)
      })
    })
  },
  click1(e){
    var index1 = e.currentTarget.dataset.index
    console.log(index1)
    this.setData({
      index1:index1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.query()
    this.setData({
      userId:wx.getStorageSync('userid')
    })
    
  },
  query(){
    this.setData({
      isLoading:true
    })
    
    wx.request({
      url: 'https://zhixiao.site:8080/goodMemory/query',
      data:{
        careHomeName:wx.getStorageSync('beadhouse'),
        page:this.data.page,
        pageSize:6
      },
      success:(res)=>{
        var recordList = res.data.data.records
        console.log(recordList)
        var recordList1 = []
        for(var i = 0;i<recordList.length;i++){
          let obj = {}
          obj.content = recordList[i].content
          obj.uploadDate = recordList[i].createdTime
          obj.userId = recordList[i].userVo.userId
          obj.id = recordList[i].id
          obj.name = recordList[i].userVo.nickname
          // obj.fileList = recordList[i].urls
          obj.fileList = this.storageImage(recordList[i].urls)

          recordList1.push(obj)
        }
        this.setData({
          recordList:[...this.data.recordList,...recordList1]
        })
      },
      complete:(res)=>{
        this.setData({
          isLoading:false
        })
        
      }
    })
  },
  storageImage(fileList) {
    for (let i = 0; i < fileList.length; i++) {
      ((index) => {
        let url = fileList[index];
        if (wx.getStorageSync(url) !== '') {
          fileList[index] = wx.getStorageSync(url);
        } else {
          wx.downloadFile({
            url: url,
            success: function (res) {
              if (res.statusCode === 200) {
                console.log(url);
                console.log(res.tempFilePath);
                fileList[index] = res.tempFilePath;
                wx.setStorageSync(url, res.tempFilePath);
              } else {
                console.log('图片下载失败');
              }
            },
            fail: function (res) {
              console.log(res);
              console.log('图片下载失败');
            }
          });
        }
      })(i);
    }
    return fileList;
  },
  delete(e){
    Dialog.confirm({
      title: '提示',
      message: '你确定要删除这条记录吗？',
    })
      .then(() => {
        // on confirm
        var index = e.currentTarget.dataset.index
        var id = this.data.recordList[index].id
        var ids = []
        ids.push(id)
        ids = ids.toString()
        console.log(ids)
        wx.request({
          url: `https://zhixiao.site:8080/goodMemory/delete?ids=${ids}`,
          success:(res)=>{
            console.log(res)
          },
          complete:(res)=>{
            this.setData({
              page:1,
              recordList:[]
            })
            this.query()

          }
        })
          })
      .catch(() => {
        // on cancel
      });
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    

    var isUpdate = wx.getStorageSync('update1')
    if(isUpdate===1){
      this.setData({
        recordList:[],
        page:1
      })
      this.query()
      wx.setStorageSync('update1', 0)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoading) return
    this.setData({
      page:this.data.page + 1
    })
    this.query()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})